package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.*;
import com.dingreading.cloud.soms.dto.QuerySubjectDto;
import com.dingreading.cloud.soms.entity.*;
import com.dingreading.cloud.soms.entity.table.MstCiStudentTableDef;
import com.dingreading.cloud.soms.mapper.MstCiStudentMapper;
import com.dingreading.cloud.soms.service.*;
import com.dingreading.cloud.soms.util.MemberBeanUtil;
import com.dingreading.cloud.soms.util.ScheduleUtil;
import com.mybatisflex.core.query.QueryCondition;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class MstCiStudentServiceImpl extends BaseServiceImpl<MstCiStudentMapper, MstCiStudent, MstCiStudentTableDef> implements MstCiStudentService {
    @Override
    protected MstCiStudentTableDef getTable() {
        return MstCiStudentTableDef.mstCiStudent;
    }

    @Resource
    private CmordersService cmordersService;
    @Resource
    private CmservicesService cmservicesService;
    @Resource
    private MembersService membersService;
    @Resource
    private CaClassService caClassService;
    @Resource
    private CaClassroomsService caClassroomsService;
    @Resource
    private CaScheduleService caScheduleService;
    @Resource
    private CaScheduleLessonService caScheduleLessonService;
    @Resource
    private CaTeacherService caTeacherService;
    @Resource
    private CaCallNamesService caCallNamesService;
    @Resource
    private SubjectsService subjectsService;
    @Resource
    private StoreProjectsService storeProjectsService;
    @Resource
    private StoreProjectStageService storeProjectStageService;
    @Resource
    private StaffService staffService;
    @Resource
    private StaffRolesService staffRolesService;
    @Resource
    private CaClassMemberService caClassMemberService;
    @Resource
    private CmpaypicsService cmpaypicsService;

    @Resource
    private MstCmorderService mstCmorderService;
    @Resource
    private MstCmservicesService mstCmservicesService;
    @Resource
    private MstCaClassService mstCaClassService;
    @Resource
    private MstCaClassroomsService mstCaClassroomsService;
    @Resource
    private MstCaScheduleService mstCaScheduleService;
    @Resource
    private MstCaScheduleLessonService mstCaScheduleLessonService;
    @Resource
    private MstCaTeacherService mstCaTeacherService;
    @Resource
    private MstCaCallNamesService mstCaCallNamesService;
    @Resource
    private MstAgencyStaffService mstAgencyStaffService;
    @Resource
    private MstCaClassMemberService mstCaClassMemberService;
    @Resource
    private MstCmpaypicsService mstCmpaypicsService;

    @Resource
    private SsMstXcxAnswersPandectService ssMstXcxAnswersPandectService;
    @Resource
    private SsMstXcxHomeworkFilesService ssMstXcxHomeworkFilesService;
    @Resource
    private SsMstXcxHomeworkFilesTrackService ssMstXcxHomeworkFilesTrackService;
    @Resource
    private SsMstXcxStudentHomeworkService ssMstXcxStudentHomeworkService;
    @Resource
    private SsMstXcxStudentLearningProgressService ssMstXcxStudentLearningProgressService;
    @Resource
    private SsMstXcxStudentQuizAnswersService ssMstXcxStudentQuizAnswersService;

    @Resource
    private MstXcxAnswersPandectService mstXcxAnswersPandectService;
    @Resource
    private MstXcxHomeworkFilesService mstXcxHomeworkFilesService;
    @Resource
    private MstXcxHomeworkFilesTrackService mstXcxHomeworkFilesTrackService;
    @Resource
    private MstXcxStudentHomeworkService mstXcxStudentHomeworkService;
    @Resource
    private MstXcxStudentLearningProgressService mstXcxStudentLearningProgressService;
    @Resource
    private MstXcxStudentQuizAnswersService mstXcxStudentQuizAnswersService;

    String mstStoreUid = "8e47c9cc0db64272b8eb56795673aca2";
    String corpUid = "211001";
    Long storeId = 4L;
    String storeUid = "7af88f7d936e476a88112f8df5517ad8";
    String storeName = "书香门第店";
    String staffUid = "1d13cce8b3d4458d887ed5f2ff73d014";
    String staffName = "樊荣";
    String staffNickName = "凡凡";
    String staffPhone = "13855454575";


    @Override
    public R<Object> transferStaff() throws Exception {
        List<MstAgencyStaff> mstAgencyStaffList = mstAgencyStaffService.listByStoreUid(mstStoreUid);
        if (CollectionUtils.isEmpty(mstAgencyStaffList)) {
            return R.fail("未查询到名师堂的员工账号");
        }
        // 获取公司下的最新username
        int usernameNum = getUsernameNum(corpUid);
        if (usernameNum <= 0)
            return R.fail("截取员工序号失败");

        List<Staff> staffList = new ArrayList<>();
        List<StaffRoles> staffRolesList = new ArrayList<>();
        List<String> pwdList = new ArrayList<>();
        for (MstAgencyStaff mstAgencyStaff : mstAgencyStaffList) {
            EnumType.StaffRoleType StaffRoleType = null;
            if (mstAgencyStaff.getRole().equals("investor") || mstAgencyStaff.getRole().equals("manager")) {
                // 投资人和店长
                StaffRoleType = EnumType.StaffRoleType.DIANZHANG;
            } else {
                // 导读
                StaffRoleType = EnumType.StaffRoleType.DAODU;
            }
            // 设置员工密码
            String staffUid = mstAgencyStaff.getStaffUid();
            // 设置员工密码
            String username = corpUid + usernameNum;
            String pwdSalt = KitUtil.getRandomPwdStr(10);
            String pwd = username + KitUtil.getRandomPwdStr(4);
            String pwdSource = StaffPwdUtil.getPwdSource(pwd, pwdSalt);
            String encodedPwd = MD5Util.MD5Encode(pwdSource);
            pwdList.add(StaffRoleType.getName() + "，账号：" + username + " 密码：" + pwd);

            Staff staff = newStaff(
                    corpUid, storeUid, storeId, staffUid, username, mstAgencyStaff.getStaffName(),
                    mstAgencyStaff.getPhone(), StaffRoleType.getAuth(), encodedPwd, pwdSalt
            );
            staffList.add(staff);

            StaffRoles staffRoles = newStaffRole(staffUid, storeUid, StaffRoleType.getName(), StaffRoleType.getRoles(), "", "系统");
            staffRolesList.add(staffRoles);

            usernameNum += 1;
            // 数字最后一位是4则跳过
            usernameNum = TextUtils.dealLastFour(usernameNum);

        }
        if (CollectionUtils.isNotEmpty(staffList))
            staffService.saveOrUpdateBatch(staffList);
        if (CollectionUtils.isNotEmpty(staffRolesList))
            staffRolesService.saveOrUpdateBatch(staffRolesList);

        return R.ok(pwdList);
    }

    @Override
    public R<Object> transferMember() {
        QueryCondition condition = table.storeUid.eq(mstStoreUid);
        List<MstCiStudent> studentList = getMapper().selectListByCondition(condition);
        if (CollectionUtils.isEmpty(studentList)) {
            return R.fail("未查询到名师堂的会员");
        }
        String password = "123456";
        String md5pass = MD5Util.MD5Encode(password, "UTF-8");

        List<Members> membersList = new ArrayList<>();
        for (MstCiStudent student : studentList) {
            List<Members> existMemberList = membersService.findByMobAndFullName(storeUid, student.getTelephone(), student.getNickname());
            if (CollectionUtils.isNotEmpty(existMemberList)) {
                Members members = existMemberList.get(0);
                // 姓名和手机号完全一样
                if (student.getTelephone().equals(members.getMob()) && student.getNickname().equals(members.getFullName())) {
                    membersService.updateDatabaseType(members.getUid(), student.getUid(), 3);
                } else {
                    Members data = saveMember(student, md5pass);
                    membersList.add(data);
                }
            } else {
                Members data = saveMember(student, md5pass);
                membersList.add(data);
            }
        }

        if (CollectionUtils.isNotEmpty(membersList)) {
            membersService.saveBatch(membersList, 50);
        }
        return R.ok();
    }

    @Transactional
    @Override
    public R<Object> transferOrder() throws Exception {
        // 查询已转移的名师堂会员
        List<Members> membersList = membersService.mstMemberList(storeUid);
        if (CollectionUtils.isEmpty(membersList)) {
            return R.fail("未查询到已转移的名师堂会员");
        }
        List<MstCmorder> mstCmorderList = mstCmorderService.listByStoreUid(mstStoreUid);
        if (CollectionUtils.isEmpty(mstCmorderList)) {
            return R.fail("未查询到名师堂的会员订单");
        }
        List<MstCmservices> mstCmservicesList = mstCmservicesService.listByStoreUid(mstStoreUid);
        if (CollectionUtils.isEmpty(mstCmservicesList)) {
            return R.fail("未查询到名师堂的订单服务");
        }


        Map<String, Members> memberMap = membersList.stream().collect(Collectors.toMap(Members::getMstUid, Function.identity()));
        Map<String, List<MstCmorder>> mstOrderMap = mstCmorderList.stream().collect(Collectors.groupingBy(MstCmorder::getStudentUid));
        Map<String, List<MstCmservices>> mstServiceMap = mstCmservicesList.stream().collect(Collectors.groupingBy(MstCmservices::getOrderNo));

        List<MstCmpaypics> mstCmpaypicsList = mstCmpaypicsService.listByStoreUidAndOrderNoIn(mstStoreUid, mstServiceMap.keySet());
        Map<String, List<MstCmpaypics>> paypicMap = mstCmpaypicsList.stream().collect(Collectors.groupingBy(MstCmpaypics::getOrderNo));

        Set<String> mstStaffUidList = new HashSet<>();
        for (MstCmorder mstCmorder : mstCmorderList) {
            mstStaffUidList.add(mstCmorder.getSaleStaffUid());
        }
        for (MstCmservices mstCmservices : mstCmservicesList) {
            mstStaffUidList.add(mstCmservices.getTeacherUid());
        }
        List<Staff> staffList = staffService.listByStoreUidAndMstUidIn(storeUid, mstStaffUidList);
        if (CollectionUtils.isEmpty(staffList)) {
            return R.fail("未查询到叮叮的员工账号");
        }
        Map<String, Staff> staffMap = staffList.stream().collect(Collectors.toMap(Staff::getUid, Function.identity()));

        List<Cmorders> newOrderList = new ArrayList<>();
        List<Cmpaypics> newPicList = new ArrayList<>();
        List<Cmservices> newServiceList = new ArrayList<>();
        List<String> duplicateOrderCode = new ArrayList<>();
        for (Map.Entry<String, Members> entry : memberMap.entrySet()) {
            String mstUid = entry.getKey();
            Members members = entry.getValue();

            List<MstCmorder> cmorders = mstOrderMap.get(mstUid);
            if (CollectionUtils.isEmpty(cmorders)) {
                System.out.println(members.getFullName() + "，会员没有订单");
            } else {
                for (MstCmorder mstCmorder : cmorders) {
                    String orderType = mstCmorder.getOrderType();
                    List<MstCmservices> cmservices = mstServiceMap.get(mstCmorder.getOrderNo());
                    if (CollectionUtils.isEmpty(cmservices)) {
                        throw new DescribeException(mstCmorder.getOrderNo() + ",订单没有服务");
                    }
                    Staff staff = null;
                    if (mstCmorder.getSaleStaffUid() != null) {
                        staff = staffMap.get(mstCmorder.getSaleStaffUid());
                        if (staff == null) {
                            throw new DescribeException(mstCmorder.getOrderNo() + ",订单没有查到签单人");
                        }
                    }
                    // 获取订单号并去重
                    String orderCode = KitUtil.getMemberServiceCode();
                    orderCode = getOrderCode(duplicateOrderCode, orderCode);
                    duplicateOrderCode.add(orderCode);
                    // 新订单
                    Cmorders savedOrder = saveOrder(members.getUid(), members.getMemberName(), orderType, orderCode, cmservices.size(), mstCmorder, staff);
                    newOrderList.add(savedOrder);

                    // 付款截图
                    List<MstCmpaypics> mstCmpaypics = paypicMap.get(mstCmorder.getOrderNo());
                    if (CollectionUtils.isNotEmpty(mstCmpaypics)) {
                        for (MstCmpaypics mstCmpaypic : mstCmpaypics) {
                            Cmpaypics cmpaypics = new Cmpaypics();
                            cmpaypics.setOrderCode(orderCode);
                            cmpaypics.setImgUrl(mstCmpaypic.getImgUrl());
                            cmpaypics.setStaffUid(staffUid);
                            newPicList.add(cmpaypics);
                        }
                    }
                    // 新服务
                    for (MstCmservices mstCmservices : cmservices) {
                        Staff readerStaff = null;
                        if (mstCmservices.getTeacherUid() != null) {
                            readerStaff = staffMap.get(mstCmservices.getTeacherUid());
                            if (readerStaff == null) {
                                throw new DescribeException(mstCmorder.getOrderNo() + ",服务没有查到导读");
                            }
                        }
                        Cmservices service = saveService(members.getUid(), members.getMemberName(), orderCode, orderType, mstCmservices, readerStaff);
                        newServiceList.add(service);
                    }
                }
            }
        }
        cmordersService.saveBatch(newOrderList);
        cmpaypicsService.saveBatch(newPicList);
        cmservicesService.saveBatch(newServiceList);
        return R.ok();
    }

    @Override
    public R<Object> transferRoom() {
        // 教室名相同，更新名师堂uid
        List<MstCaClassrooms> mstClassroomsList = mstCaClassroomsService.listByStoreUid(mstStoreUid);
        if (CollectionUtils.isEmpty(mstClassroomsList)) {
            return R.fail("未查询到名师堂的教室数据");
        }

        List<String> roomNameList = mstClassroomsList.stream().map(MstCaClassrooms::getRoomName).collect(Collectors.toList());
        List<CaClassrooms> caClassroomsList = caClassroomsService.listByStoreUidAndRoomNameIn(storeUid, roomNameList);
        Map<String, CaClassrooms> classroomsMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(caClassroomsList)) {
            classroomsMap = caClassroomsList.stream().collect(Collectors.toMap(CaClassrooms::getRoomName, Function.identity()));
        }

        List<CaClassrooms> newList = new ArrayList<>();
        for (MstCaClassrooms mstCaClassrooms : mstClassroomsList) {
            CaClassrooms caClassrooms = classroomsMap.get(mstCaClassrooms.getRoomName());
            if (caClassrooms != null) {
                caClassroomsService.updateUid(caClassrooms.getId(), mstCaClassrooms.getUid());
            } else {
                CaClassrooms data = getCaClassrooms(mstCaClassrooms);
                newList.add(data);
            }
        }
        if (CollectionUtils.isNotEmpty(newList)) {
            caClassroomsService.saveBatch(newList);
        }
        return R.ok();
    }


    @Override
    public R<Object> transferClass() throws DescribeException {
        // 班级数据迁移
        List<MstCaClass> mstClassList = mstCaClassService.listByStoreUid(mstStoreUid);
        if (CollectionUtils.isEmpty(mstClassList)) {
            return R.fail("未查询到名师堂的班级数据");
        }

        List<String> roomUidList = mstClassList.stream().map(MstCaClass::getRoomUid).collect(Collectors.toList());
        List<CaClassrooms> caClassroomsList = caClassroomsService.listByStoreUidAndRoomUidIn(storeUid, roomUidList);
        if (CollectionUtils.isEmpty(caClassroomsList)) {
            return R.fail("未查询到叮叮的教室信息");
        }
        Map<String, CaClassrooms> classroomsMap = caClassroomsList.stream().collect(Collectors.toMap(CaClassrooms::getUid, Function.identity()));

        List<CaClass> newClassList = new ArrayList<>();
        for (MstCaClass mstCaClass : mstClassList) {
            CaClass data = new CaClass();
            data.setCorpUid(corpUid);
            data.setStoreUid(storeUid);
            data.setStoreName(storeName);
            data.setUid(mstCaClass.getUid());

            QuerySubjectDto querySubjectDto = getSubjectName(mstCaClass.getProjectName(), "");
            StoreProjects storeProjects = storeProjectsService.getByProjectName(querySubjectDto.getMainSubjectName());
            if (storeProjects == null) {
                throw new DescribeException("未查询到服务");
            }
            StoreProjectStage storeProjectStage = storeProjectStageService.getByProjectUidAndStageName(storeProjects.getUid(), mstCaClass.getStageName());
            if (storeProjectStage == null) {
                throw new DescribeException("未查询到服务的阶段");
            }
            data.setStageUid(storeProjectStage.getUid());
            data.setStageName(storeProjectStage.getStageName());

            CaClassrooms caClassrooms = classroomsMap.get(mstCaClass.getRoomUid());
            if (caClassrooms == null) {
                throw new DescribeException("未查询到教室");
            }

            data.setRoomId(caClassrooms.getId());
            data.setRoomUid(caClassrooms.getUid());
            data.setRoomName(caClassrooms.getRoomName());
            data.setTeacherUid(mstCaClass.getTeacherUid());
            data.setTeacherName(mstCaClass.getTeacherName());

            data.setProjectUid(storeProjects.getUid());
            data.setProductName(storeProjects.getProjectName());
            data.setFreeStatus(mstCaClass.getFreeStatus());
            data.setStudentNum(mstCaClass.getStudentNum());
            data.setScheduleNum(mstCaClass.getScheduleNum());
            data.setCloseStatus(1);
            data.setEnabled(1);
            data.setAddStaffUid(staffUid);
            data.setAddStaffName(staffName);
            data.setClassName(mstCaClass.getClassName());
            data.setRemark(mstCaClass.getRemark());
            data.setLastStaffUid(staffUid);
            data.setLastStaffName(staffName);
            newClassList.add(data);
        }
        caClassService.saveBatch(newClassList);
        return R.ok();
    }

    @Override
    public R<Object> transferClassMember() throws DescribeException {
        List<MstCaClassMember> mstCaClassMemberList = mstCaClassMemberService.listByStoreUid(mstStoreUid);
        if (CollectionUtils.isEmpty(mstCaClassMemberList)) {
            return R.fail("未查询到名师堂的班级会员数据");
        }
        List<String> classUidList = mstCaClassMemberList.stream().map(MstCaClassMember::getClassUid).collect(Collectors.toList());
        List<String> studentUidList = mstCaClassMemberList.stream().map(MstCaClassMember::getStudentUid).collect(Collectors.toList());

        List<CaClass> caClassList = caClassService.listByUidIn(storeUid, classUidList);
        if (CollectionUtils.isEmpty(caClassList)) {
            return R.fail("未查询到叮叮的班级信息");
        }

        List<Members> membersList = membersService.listByStoreUidAndMstUid(storeUid, studentUidList);
        if (CollectionUtils.isEmpty(caClassList)) {
            return R.fail("未查询到叮叮的会员信息");
        }

        Map<String, CaClass> caClassMap = caClassList.stream().collect(Collectors.toMap(CaClass::getUid, Function.identity()));
        Map<String, Members> memberMap = membersList.stream().collect(Collectors.toMap(Members::getMstUid, Function.identity()));

        List<CaClassMember> newList = new ArrayList<>();
        Map<String, List<MstCaClassMember>> mstClassMemberMap = mstCaClassMemberList.stream().collect(Collectors.groupingBy(MstCaClassMember::getClassUid));
        for (Map.Entry<String, List<MstCaClassMember>> entry : mstClassMemberMap.entrySet()) {
            String classUid = entry.getKey();
            for (MstCaClassMember mstCaClassMember : entry.getValue()) {
                CaClassMember data = new CaClassMember();
                data.setCorpUid(corpUid);
                data.setStoreUid(storeUid);
                data.setStoreName(storeName);

                CaClass caClass = caClassMap.get(classUid);
                if (caClass == null) {
                    return R.fail("未查询到班级信息");
                }
                data.setClassId(caClass.getId());
                data.setClassUid(caClass.getUid());

                Members members = memberMap.get(mstCaClassMember.getStudentUid());
                if (members == null) {
                    return R.fail("未查询到会员信息");
                }
                data.setMemberUid(members.getUid());
                data.setEnabled(1);
                data.setAddStaffUid(staffUid);
                data.setAddStaffName(staffName);
                data.setLastStaffUid(staffUid);
                data.setLastStaffName(staffName);
                newList.add(data);
            }
        }
        caClassMemberService.saveBatch(newList);

        return R.ok();
    }

    @Override
    public R<Object> transferLesson() throws DescribeException {
        List<MstCaSchedule> mstCaScheduleList = mstCaScheduleService.listByStoreUid(mstStoreUid);
        if (CollectionUtils.isEmpty(mstCaScheduleList)) {
            return R.fail("未查询到名师堂课表数据");
        }
        List<MstCaScheduleLesson> mstCaScheduleLessonList = mstCaScheduleLessonService.listByStoreUid(mstStoreUid);
        if (CollectionUtils.isEmpty(mstCaScheduleLessonList)) {
            return R.fail("未查询到名师堂课次数据");
        }
        List<MstCaTeacher> mstCaTeacherList = mstCaTeacherService.listByStoreUid(mstStoreUid);
        if (CollectionUtils.isEmpty(mstCaTeacherList)) {
            return R.fail("未查询到名师堂的课表老师数据");
        }

        Set<String> teacherUidList = new HashSet<>();
        List<String> classUidList = new ArrayList<>();
        List<String> roomUidList = new ArrayList<>();
        for (MstCaSchedule mstCaSchedule : mstCaScheduleList) {
            teacherUidList.add(mstCaSchedule.getTeacherUid());
            classUidList.add(mstCaSchedule.getClassUid());
            roomUidList.add(mstCaSchedule.getRoomUid());
        }
        for (MstCaScheduleLesson mstCaScheduleLesson : mstCaScheduleLessonList) {
            teacherUidList.add(mstCaScheduleLesson.getCloseStaffUid());
        }
        // 查门店员工账号
        List<Staff> staffList = staffService.listByStoreUidAndMstUidIn(storeUid, teacherUidList);
        if (CollectionUtils.isEmpty(staffList)) {
            return R.fail("未查询到叮叮的员工账号");
        }
        Map<String, Staff> staffMap = staffList.stream().collect(Collectors.toMap(Staff::getUid, Function.identity()));

        // 查班级
        List<CaClass> caClassList = caClassService.listByUidIn(storeUid, classUidList);
        if (CollectionUtils.isEmpty(caClassList)) {
            return R.fail("未查询到叮叮的班级");
        }
        Map<String, CaClass> classMap = caClassList.stream().collect(Collectors.toMap(CaClass::getUid, Function.identity()));

        List<CaClassrooms> caClassroomsList = caClassroomsService.listByStoreUidAndRoomUidIn(storeUid, roomUidList);
        if (CollectionUtils.isEmpty(caClassroomsList)) {
            return R.fail("未查询到叮叮的教室");
        }
        Map<String, CaClassrooms> classroomMap = caClassroomsList.stream().collect(Collectors.toMap(CaClassrooms::getUid, Function.identity()));

        List<CaTeacher> newTeacherList = new ArrayList<>();
        for (MstCaTeacher teacher : mstCaTeacherList) {
            Staff staff = staffMap.get(teacher.getStaffUid());
            if (staff == null) {
                return R.fail("未查询到员工信息");
            }

            CaTeacher data = saveTeachers(teacher, staff);
            newTeacherList.add(data);
        }

        Map<String, List<MstCaScheduleLesson>> lessMap = mstCaScheduleLessonList.stream().collect(Collectors.groupingBy(MstCaScheduleLesson::getScheduleUid));

        List<CaSchedule> newScheduleList = new ArrayList<>();
        List<CaScheduleLesson> newScheduleLessonList = new ArrayList<>();
        for (MstCaSchedule schedule : mstCaScheduleList) {
            // 创建
            Staff staff = staffMap.get(schedule.getTeacherUid());
            if (staff == null) {
                return R.fail("未查询到员工信息");
            }
            CaClass caClass = classMap.get(schedule.getClassUid());
            if (caClass == null) {
                return R.fail("未查询到班级信息");
            }
            CaClassrooms classrooms = classroomMap.get(schedule.getRoomUid());
            if (classrooms == null) {
                return R.fail("未查询到教室信息");
            }

            List<MstCaScheduleLesson> lessonList = lessMap.get(schedule.getScheduleUid());
            if (CollectionUtils.isEmpty(lessonList)) {
                return R.fail("未查询到课表下的课次信息");
            }

            QuerySubjectDto querySubjectDto = getSubjectName(schedule.getProjectName(), "");
            StoreProjects storeProjects = storeProjectsService.getByProjectName(querySubjectDto.getMainSubjectName());
            if (storeProjects == null) {
                return R.fail("未查询到服务信息");
            }
            StoreProjectStage storeProjectStage = storeProjectStageService.getByProjectUidAndStageName(storeProjects.getUid(), querySubjectDto.getStageName());
            if (storeProjectStage == null) {
                throw new DescribeException("未查询到服务的阶段");
            }
            CaSchedule caSchedule = saveSchedule(schedule, staff, caClass, storeProjects, storeProjectStage, classrooms);
            newScheduleList.add(caSchedule);

            for (MstCaScheduleLesson lesson : lessonList) {
                Staff closeStaff = null;
                if (lesson.getCloseStaffUid() != null) {
                    closeStaff = staffMap.get(lesson.getCloseStaffUid());
                    if (closeStaff == null) {
                        throw new DescribeException("未查询到结课员工信息");
                    }
                }
                CaScheduleLesson caScheduleLesson = saveLesson(lesson, staff, caClass, storeProjects, storeProjectStage, classrooms, closeStaff);
                newScheduleLessonList.add(caScheduleLesson);
            }
        }

        caTeacherService.saveBatch(newTeacherList);
        caScheduleService.saveBatch(newScheduleList);
        caScheduleLessonService.saveBatch(newScheduleLessonList);

        return R.ok();
    }

    @Override
    public R<Object> transferCallName() {
        List<MstCaCallNames> mstCaCallNamesList = mstCaCallNamesService.listByStoreUid(mstStoreUid);
        if (CollectionUtils.isEmpty(mstCaCallNamesList)) {
            return R.fail("未查询名师堂的点名记录");
        }

        List<Members> membersList = membersService.mstMemberList(storeUid);
        if (CollectionUtils.isEmpty(membersList)) {
            return R.fail("未查询已转移到叮叮的会员信息");
        }
        Map<String, Members> membersMap = membersList.stream().collect(Collectors.toMap(Members::getMstUid, Function.identity()));

        List<String> lessonUidList = mstCaCallNamesList.stream().map(MstCaCallNames::getLessonUid).collect(Collectors.toList());
        List<CaScheduleLesson> lessonList = caScheduleLessonService.listByStoreUidAndLessonUidIn(storeUid, lessonUidList);
        if (CollectionUtils.isEmpty(lessonList)) {
            return R.fail("未查询已转移到叮叮的课次信息");
        }
        Map<String, CaScheduleLesson> lessonMap = lessonList.stream().collect(Collectors.toMap(CaScheduleLesson::getLessonUid, Function.identity()));

        List<CaCallNames> newList = new ArrayList<>();
        for (MstCaCallNames call : mstCaCallNamesList) {
            Members members = membersMap.get(call.getStudentUid());
            if (members == null) {
                return R.fail("未查询到会员信息");
            }
            CaScheduleLesson lesson = lessonMap.get(call.getLessonUid());
            if (lesson == null) {
                return R.fail("未查询到课次信息");
            }
            CaCallNames data = saveCall(call, members, lesson);
            newList.add(data);
        }
        caCallNamesService.saveBatch(newList);
        return R.ok();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R<Object> transferInClass() {

        List<SsMstXcxStudentQuizAnswers> ssXcxStudentQuizAnswersList = ssMstXcxStudentQuizAnswersService.getByMstStoreUid(mstStoreUid);
        if (CollectionUtils.isEmpty(ssXcxStudentQuizAnswersList)) {
            return R.fail("未查询到小程序课堂巩固学生答题记录数据");
        }
        List<String> studentUidList = ssXcxStudentQuizAnswersList.stream().map(SsMstXcxStudentQuizAnswers::getStudentUid).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(studentUidList))
            return R.fail("未查询到小程序课堂巩固学生答题记录的会员信息");
        List<SsMstXcxAnswersPandect> ssXcxAnswersPandectList = ssMstXcxAnswersPandectService.getByStudentUidList(studentUidList);
        if (CollectionUtils.isEmpty(ssXcxAnswersPandectList)) {
            return R.fail("未查询到小程序课堂巩固学生答题记录总览数据");
        }

        List<Members> membersList = membersService.mstMemberList(storeUid);
        if (CollectionUtils.isEmpty(membersList)) {
            return R.fail("未查询已转移到叮叮的会员信息");
        }
        Map<String, Members> membersMap = membersList.stream().collect(Collectors.toMap(Members::getMstUid, Function.identity()));

        List<MstXcxAnswersPandect> mstXcxAnswersPandectList = new ArrayList<>();
        List<MstXcxStudentQuizAnswers> mstXcxStudentQuizAnswersList = new ArrayList<>();

        dealInClass(ssXcxAnswersPandectList, mstXcxAnswersPandectList, ssXcxStudentQuizAnswersList, mstXcxStudentQuizAnswersList, membersMap);

        if (!CollectionUtils.isEmpty(mstXcxAnswersPandectList)) {
            boolean b = mstXcxAnswersPandectService.saveBatch(mstXcxAnswersPandectList);
            if (!b) {
                throw new RuntimeException("保存失败！");
            }
        }
        if (!CollectionUtils.isEmpty(mstXcxStudentQuizAnswersList)) {
            boolean b = mstXcxStudentQuizAnswersService.saveBatch(mstXcxStudentQuizAnswersList);
            if (!b) {
                throw new RuntimeException("保存失败！");
            }
        }
        return R.ok();
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public R<Object> transferAfterClass() {
        List<SsMstXcxStudentHomework> ssMstXcxStudentHomeworkList = ssMstXcxStudentHomeworkService.getByMstStoreUid(mstStoreUid);
        if (CollectionUtils.isEmpty(ssMstXcxStudentHomeworkList)) {
            return R.fail("未查询到小程序后作业数据");
        }
        List<String> homeworkUidList = ssMstXcxStudentHomeworkList.stream().map(SsMstXcxStudentHomework::getUid).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(homeworkUidList)) {
            return R.fail("未查询到小程序后作业的会员信息");
        }
        List<SsMstXcxHomeworkFiles> ssMstXcxHomeworkFilesList = ssMstXcxHomeworkFilesService.getByHomeworkUidList(homeworkUidList);
        if (CollectionUtils.isEmpty(ssMstXcxHomeworkFilesList)) {
            return R.fail("未查询到小程序课后作业文件数据");
        }
        List<SsMstXcxHomeworkFilesTrack> ssMstXcxHomeworkFilesTrackList = ssMstXcxHomeworkFilesTrackService.getByHomeworkUidList(homeworkUidList);
        if (CollectionUtils.isEmpty(ssMstXcxHomeworkFilesTrackList)) {
            return R.fail("未查询到小程序课后作业文件追踪数据");
        }
        List<Members> membersList = membersService.mstMemberList(storeUid);
        if (CollectionUtils.isEmpty(membersList)) {
            return R.fail("未查询已转移到叮叮的会员信息");
        }
        Map<String, Members> membersMap = membersList.stream().collect(Collectors.toMap(Members::getMstUid, Function.identity()));

        List<MstXcxStudentHomework> mstXcxStudentHomeworkList = new ArrayList<>();
        List<MstXcxHomeworkFiles> mstXcxHomeworkFilesList = new ArrayList<>();
        List<MstXcxHomeworkFilesTrack> mstXcxHomeworkFilesTrackList = new ArrayList<>();

        dealAfterClass(
                ssMstXcxStudentHomeworkList, mstXcxStudentHomeworkList,
                ssMstXcxHomeworkFilesList, mstXcxHomeworkFilesList,
                ssMstXcxHomeworkFilesTrackList, mstXcxHomeworkFilesTrackList,
                membersMap
        );

        if (!CollectionUtils.isEmpty(mstXcxStudentHomeworkList)) {
            boolean b = mstXcxStudentHomeworkService.saveBatch(mstXcxStudentHomeworkList);
            if (!b) {
                throw new RuntimeException("保存小程序学生课后作业失败！");
            }
        }
        if (!CollectionUtils.isEmpty(mstXcxHomeworkFilesList)) {
            boolean b = mstXcxHomeworkFilesService.saveBatch(mstXcxHomeworkFilesList);
            if (!b) {
                throw new RuntimeException("保存小程序学生课后作业文件失败！");
            }
        }
        if (!CollectionUtils.isEmpty(mstXcxHomeworkFilesTrackList)) {
            boolean b = mstXcxHomeworkFilesTrackService.saveBatch(mstXcxHomeworkFilesTrackList);
            if (!b) {
                throw new RuntimeException("保存小程序学生课后作业文件追踪失败！");
            }
        }
        return R.ok();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R<Object> transferLearning() {
        List<SsMstXcxStudentLearningProgress> ssMstXcxStudentLearningProgressList = ssMstXcxStudentLearningProgressService.getByMstStoreUid(mstStoreUid);
        if (CollectionUtils.isEmpty(ssMstXcxStudentLearningProgressList)) {
            return R.fail("未查询到小程序学习轨迹数据");
        }
        List<Members> membersList = membersService.mstMemberList(storeUid);
        if (CollectionUtils.isEmpty(membersList)) {
            return R.fail("未查询已转移到叮叮的会员信息");
        }
        Map<String, Members> membersMap = membersList.stream().collect(Collectors.toMap(Members::getMstUid, Function.identity()));
        List<MstXcxStudentLearningProgress> mstXcxStudentLearningProgressList = new ArrayList<>();

        dealLearningProgress(ssMstXcxStudentLearningProgressList, mstXcxStudentLearningProgressList, membersMap);

        if (!CollectionUtils.isEmpty(mstXcxStudentLearningProgressList)) {
            boolean b = mstXcxStudentLearningProgressService.saveBatch(mstXcxStudentLearningProgressList);
            if (!b) {
                throw new RuntimeException("保存小程序学习轨迹数据失败！");
            }
        }
        return R.ok();
    }

    /**
     * 创建点名记录
     *
     * @return
     */
    private CaCallNames saveCall(MstCaCallNames mstCall, Members members, CaScheduleLesson lesson) {
        CaCallNames data = new CaCallNames();
        data.setCorpUid(corpUid);
        data.setStoreUid(storeUid);
        data.setStoreName(storeName);
        data.setUid(mstCall.getUid());
        data.setScheduleUid(mstCall.getScheduleUid());
        data.setLessonUid(mstCall.getLessonUid());
        data.setFreeStatus(mstCall.getFreeStatus());
        data.setMemberId(members.getId());
        data.setMemberUid(members.getUid());
        data.setMemberName(members.getMemberName());
        data.setMob(members.getMob());
        data.setAddStaffUid(staffUid);
        data.setAddStaffName(staffName);
        Integer studentType = mstCall.getStudentType();
        if (null != studentType) {
            data.setStudentType(studentType);
            data.setStudentTypeName(ScheduleUtil.studentTypeNames[studentType - 1]);
        } else {
            data.setStudentType(1);
            data.setStudentTypeName("活动会员");
        }
        data.setCallStatus(mstCall.getCallStatus());
        data.setCallStatusName(mstCall.getCallStatusName());
        data.setCallTime(mstCall.getCallTime());
        data.setCloseTime(mstCall.getCloseTime());
        data.setCloseStaffUid(mstCall.getCloseStaffUid());
        data.setCloseStaffName(mstCall.getCloseStaffName());

        data.setStatus(mstCall.getStatus());
        data.setMakeupStatus(mstCall.getMakeupStatus());
        data.setTeachDate(mstCall.getTeachDate());
        data.setWeekNum(mstCall.getWeekNum());
        data.setWeekName(mstCall.getWeekName());
        data.setBeginTime(mstCall.getBeginTime());
        data.setEndTime(mstCall.getEndTime());

        data.setClassId(lesson.getClassId());
        data.setClassUid(lesson.getClassUid());
        data.setClassName(lesson.getClassName());
        data.setProjectUid(lesson.getProjectUid());
        data.setProductName(lesson.getProductName());
        data.setStageUid(lesson.getStageUid());
        data.setStageName(lesson.getStageName());
        data.setRoomId(lesson.getRoomId());
        data.setRoomUid(lesson.getRoomUid());
        data.setRoomName(lesson.getRoomName());
        data.setTeacherUid(lesson.getTeacherUid());
        data.setTeacherName(lesson.getTeacherName());
        data.setUsernames(lesson.getUsernames());

        data.setHourType(mstCall.getHourType());
        data.setHourTypeName(mstCall.getHourTypeName());
        data.setOrderCode(mstCall.getOrderCode());
        data.setOrderType(mstCall.getOrderType());
        data.setUnitAmount(mstCall.getUnitAmount());
        data.setServiceUid(mstCall.getServiceUid());

        data.setContent(mstCall.getContent());
        data.setRemark(mstCall.getRemark());
        data.setLastStaffUid(staffUid);
        data.setLastStaffName(staffName);
        return data;
    }


    /**
     * 创建活动表的主讲导读
     */
    private CaTeacher saveTeachers(MstCaTeacher teacher, Staff staff) {
        CaTeacher data = new CaTeacher();
        data.setCorpUid(corpUid);
        data.setStoreUid(storeUid);
        data.setScheduleUid(teacher.getScheduleUid());
        data.setLessonUid(teacher.getLessonUid());
        data.setTeacherType(teacher.getTeacherType());
        data.setUsername(staff.getUsername());
        data.setStaffUid(staff.getUid());
        data.setFullName(staff.getFullName());
        data.setNickName(staff.getNickName());
        data.setPubPhone(staff.getPubPhone());
        data.setPriPhone(staff.getPriPhone());
        data.setAddStaffUid(staffUid);
        data.setAddStaffName(staffName);
        return data;
    }

    /**
     * 创建活动计划
     */
    private CaSchedule saveSchedule(
            MstCaSchedule schedule, Staff staff, CaClass caClass, StoreProjects storeProjects,
            StoreProjectStage storeProjectStage, CaClassrooms classrooms
    ) {
        CaSchedule data = new CaSchedule();
        data.setCorpUid(corpUid);
        data.setStoreUid(storeUid);
        data.setStoreName(storeName);
        data.setScheduleUid(schedule.getScheduleUid());
        data.setFreeStatus(schedule.getFreeStatus());
        data.setStatus(schedule.getStatus());
        data.setUseLessonNum(schedule.getUseLessonNum());
        data.setAddStaffUid(staffUid);
        data.setAddStaffName(staffName);

        data.setClassId(caClass.getId());
        data.setClassUid(schedule.getClassUid());
        data.setClassName(schedule.getClassName());

        data.setProjectUid(storeProjects.getUid());
        data.setProductName(schedule.getProjectName());
        data.setStageUid(storeProjectStage.getUid());
        data.setStageName(schedule.getStageName());

        data.setRoomId(classrooms.getId());
        data.setRoomUid(classrooms.getUid());
        data.setRoomName(classrooms.getRoomName());

        data.setTeacherUid(staff.getUsername());
        data.setTeacherName(staff.getFullName());
        data.setUsernames("," + staff.getUsername() + ",");

        data.setTeachDates(schedule.getTeachDates());
        data.setBeginTime(schedule.getBeginTime());
        data.setEndTime(schedule.getEndTime());
        data.setLessonNum(schedule.getLessonNum());
        data.setContent(schedule.getContent());
        data.setStudentNum(schedule.getStudentNum());
        data.setRemark(schedule.getRemark());
        data.setLastStaffUid(staffUid);
        data.setLastStaffName(staffName);
        return data;
    }

    /**
     * 创建课次
     */
    private CaScheduleLesson saveLesson(
            MstCaScheduleLesson lesson, Staff staff, CaClass caClass, StoreProjects storeProjects,
            StoreProjectStage storeProjectStage, CaClassrooms classrooms, Staff closeStaff
    ) {
        CaScheduleLesson data = new CaScheduleLesson();
        data.setLessonUid(lesson.getLessonUid());
        data.setCorpUid(corpUid);
        data.setStoreUid(storeUid);
        data.setStoreName(storeName);
        data.setScheduleUid(lesson.getScheduleUid());
        data.setTeachDate(lesson.getTeachDate());
        data.setWeekNum(lesson.getWeekNum());
        data.setWeekName(lesson.getWeekName());
        data.setBeginTime(lesson.getBeginTime());
        data.setEndTime(lesson.getEndTime());

        data.setClassId(caClass.getId());
        data.setClassUid(lesson.getClassUid());
        data.setClassName(lesson.getClassName());

        data.setProjectUid(storeProjects.getUid());
        data.setProductName(lesson.getProjectName());
        data.setStageUid(storeProjectStage.getUid());
        data.setStageName(lesson.getStageName());

        data.setRoomId(classrooms.getId());
        data.setRoomUid(classrooms.getUid());
        data.setRoomName(classrooms.getRoomName());

        data.setTeacherUid(staff.getUsername());
        data.setTeacherName(staff.getFullName());
        data.setUsernames("," + staff.getUsername() + ",");

        data.setContent(lesson.getContent());
        data.setStudentNum(lesson.getStudentNum());
        data.setStudentTempNum(lesson.getStudentTempNum());
        data.setReachNum(lesson.getReachNum());
        data.setLateNum(lesson.getLateNum());
        data.setAskLeaveNum(lesson.getAskLeaveNum());
        data.setNotReachNum(lesson.getNotReachNum());
        data.setNoHourNum(lesson.getNoHourNum());
        data.setRemark(null);
        data.setFreeStatus(lesson.getFreeStatus());
        data.setStatus(lesson.getStatus());
        data.setAddStaffUid(staffUid);
        data.setAddStaffName(staffName);
        data.setLastStaffUid(staffUid);
        data.setLastStaffName(staffName);
        data.setCallTime(lesson.getCallTime());
        data.setCloseTime(lesson.getCloseTime());
        if (closeStaff != null) {
            data.setCloseStaffUid(closeStaff.getUid());
            data.setCloseStaffName(lesson.getCloseStaffName());
        }
        return data;
    }

    /**
     * 获取订单号并去重
     */
    private String getOrderCode(List<String> duplicateOrderCode, String orderCode) {
        if (duplicateOrderCode.contains(orderCode)) {
            orderCode = KitUtil.getMemberServiceCode();
            getOrderCode(duplicateOrderCode, orderCode);
        }
        return orderCode;
    }

    /**
     * 创建订单
     *
     * @return
     */
    private Cmorders saveOrder(String memberUid, String memberName, String orderType, String orderCode, int serviceNum, MstCmorder mstCmorder, Staff staff) {
        Cmorders data = new Cmorders();
        data.setCorpUid(corpUid);
        data.setStoreUid(storeUid);
        data.setStoreName(storeName);
        data.setAddStaffUid(staffUid);
        data.setAddStaffName(staffName);
        data.setLastStaffUid(staffUid);
        data.setLastStaffName(staffName);
        data.setOrderCode(orderCode);
        data.setMemberUid(memberUid);
        data.setMemberName(memberName);
        data.setStatus(0);
        data.setStatusName("待审批");
        // 正常
        data.setInoutType(1);
        data.setInoutTypeName("收入");
        data.setOrderState(1);
        data.setOrderStateName("正常");
        data.setOrderType(orderType);
        data.setContractCode("");
        data.setOrderAmount(mstCmorder.getOrderAmount());
        data.setGiftGotten(mstCmorder.getGiftGotten());
        data.setGiftRemark(mstCmorder.getGiftRemark());
        data.setOrderRemark(mstCmorder.getOrderRemark());
        data.setPayChannelCode(mstCmorder.getPayChannelCode());
        data.setOrderDate(mstCmorder.getOrderDate());
        data.setServiceNum(serviceNum);

        if (staff != null) {
            data.setSaleStaffUid(staff.getUid());
            data.setSaleStaffFullName(staff.getFullName());
            data.setSaleStaffPriPhone(staff.getPriPhone());
            data.setSaleStaffPubPhone(staff.getPubPhone());
            data.setSaleStaffNickName(staff.getNickName());
        }
        data.setDatabaseType(2);
        return data;
    }

    /**
     * 创建服务
     *
     * @return
     */
    private Cmservices saveService(
            String memberUid, String memberName, String orderCode, String orderType, MstCmservices mstCmservices,
            Staff readerStaff
    ) throws DescribeException {
        Cmservices data = new Cmservices();
        data.setUid(mstCmservices.getUid());
        data.setCorpUid(corpUid);
        data.setStoreUid(storeUid);
        data.setStoreName(storeName);
        data.setFromStoreUid(storeUid);
        data.setFromStoreName(storeName);
        data.setOrderCode(orderCode);
        data.setOrderType(orderType);
        data.setMemberUid(memberUid);
        data.setMemberName(memberName);
        data.setStatus(EnumType.ServiceStatus.OPEN_CARD.getStatus());
        data.setStatusName(EnumType.ServiceStatus.OPEN_CARD.getName());
        data.setChangeStatus(EnumType.ServiceStatus.NO_CHANGE.getStatus());
        data.setEnding(0);
        data.setEndingType(0);
        data.setEndingTime(null);
        data.setEndingRemark(null);

        data.setServiceDays(150);
        data.setLeftDays(150);
        data.setAddStaffUid(staffUid);
        data.setAddStaffName(staffName);
        data.setLastStaffUid(staffUid);
        data.setLastStaffName(staffName);

        if (readerStaff != null) {
            data.setStaffUid(readerStaff.getUid());
            data.setStaffFullName(readerStaff.getFullName());
            data.setStaffNickName(readerStaff.getNickName());
            data.setStaffPriPhone(readerStaff.getPriPhone());
        }

        String projectName = mstCmservices.getProjectName();
        QuerySubjectDto querySubjectDto = getSubjectName(projectName, mstCmservices.getStageName());

        StoreProjects storeProjects = storeProjectsService.getByProjectName(querySubjectDto.getMainSubjectName());
        if (storeProjects == null) {
            throw new DescribeException("未查询到服务");
        }
        data.setProductCode(storeProjects.getId() + "");
        data.setProjectUid(storeProjects.getUid());
        data.setProductName(storeProjects.getProjectName());

        if (mstCmservices.getActivateAmount() > 0) {
            Subjects mainSubject = subjectsService.findByMainSubjectName(querySubjectDto.getMainSubjectName());
            if (mainSubject == null) {
                throw new DescribeException("未查询到主学科");
            }
            data.setMainSubjectUid(mainSubject.getUid());
            data.setMainSubjectName(mainSubject.getSubjectName());
            Subjects subjects = subjectsService.findByMainSubjectIdAndSubjectName(mainSubject.getId(), querySubjectDto.getSubjectName());
            if (subjects == null) {
                throw new DescribeException("未查询到子学科");
            }
            data.setSubjectUid(subjects.getUid());
            data.setSubjectName(subjects.getSubjectName());
            if (StringUtils.isNotBlank(querySubjectDto.getStageName())) {
                Subjects stageSubject = subjectsService.findByMainSubjectIdAndSubjectName(subjects.getId(), querySubjectDto.getStageName());
                if (stageSubject == null) {
                    throw new DescribeException("未查询到最后一级学科");
                }
                data.setStageUid(stageSubject.getUid());
                data.setStageName(stageSubject.getSubjectName());
            }
            data.setActivateAmount(mstCmservices.getActivateAmount());
            data.setPayType(mstCmservices.getPayType());
        }

        data.setLessonStatus(1);
        data.setFreeStatus(1);
        data.setTotalLessons(mstCmservices.getTotalLessons());
        data.setLeftLessons(mstCmservices.getLeftLessons());
        data.setTotalAmount(mstCmservices.getTotalAmount());
        data.setUnitAmount(mstCmservices.getUnitAmount());
        data.setIsGift(0);
        data.setBeginDate(mstCmservices.getBeginDate());
        data.setEndDate(mstCmservices.getEndDate());
        data.setChangeDate(mstCmservices.getBeginDate());
        data.setRemarks(mstCmservices.getStageName());
        data.setDatabaseType(2);
        return data;
    }

    private QuerySubjectDto getSubjectName(String projectName, String stageName) throws DescribeException {
        QuerySubjectDto subjectDto = new QuerySubjectDto();
        if (projectName.contains("秋季")) {
            subjectDto.setMainSubjectName("双思思维数学");
            subjectDto.setSubjectName("秋季");
            subjectDto.setStageName(stageName);
        } else if (projectName.contains("冬季")) {
            subjectDto.setMainSubjectName("双思思维数学");
            subjectDto.setSubjectName("冬季");
            subjectDto.setStageName(stageName);
        } else if (projectName.contains("计算")) {
            subjectDto.setMainSubjectName("计算通关训练营");
            subjectDto.setSubjectName(stageName);
        } else {
            throw new DescribeException("未查询到服务名称，" + projectName);
        }
        return subjectDto;
    }

    /**
     * 创建会员
     */
    private Members saveMember(MstCiStudent student, String md5pass) {
        Members data = new Members();
        data.setCorpUid(corpUid);
        data.setStoreId(storeId);
        data.setStoreUid(storeUid);
        data.setStoreName(storeName);
        data.setAddTime(student.getAddTime());
        data.setEditTime(student.getEditTime());
        data.setAddStaffUid(staffUid);
        data.setAddStaffName(staffName);
        data.setAddNickName(staffNickName);
        data.setLastStaffUid(staffUid);
        data.setLastStaffName(staffName);
        data.setLastNickName(staffNickName);
        data.setUid(student.getUid());
        data.setFullName(student.getNickname());
        data.setMemberName(MemberBeanUtil.getMemberName(student.getNickname()));
        data.setNickName(student.getNickname());
        data.setMob(student.getTelephone());
        data.setSex(0);
        data.setGrade(student.getGrade());
        data.setEnabled(1);
        data.setReadBooks(0);
        data.setReadDays(0);
        data.setReadWords(0d);
        data.setFaceId("");
        data.setFaceUpStatus(-1);
        data.setFaceImg("");
        data.setSource("");
        data.setValidStatus(1);
        data.setFromStoreUid(storeUid);
        data.setFromStoreName(storeName);
        data.setDatabaseType(2);
        data.setMstUid(student.getUid());
        data.setPassword(md5pass);
        return data;
    }


    /**
     * 创建班级
     */
    private CaClassrooms getCaClassrooms(MstCaClassrooms mstCaClassrooms) {
        CaClassrooms data = new CaClassrooms();
        data.setCorpUid(corpUid);
        data.setStoreUid(storeUid);
        data.setStoreName(storeName);
        data.setUid(KitUtil.getUuid());
        data.setEnabled(1);
        data.setAddStaffUid(staffUid);
        data.setAddStaffName(staffName);
        data.setRoomName(mstCaClassrooms.getRoomName());
        data.setCapacityNum(mstCaClassrooms.getCapacityNum());
        data.setLastStaffUid(staffUid);
        data.setLastStaffName(staffName);
        return data;
    }

    /**
     * 获取公司下的员工账号的序号
     */
    private int getUsernameNum(String corpUid) {
        int usernameNum = 1;
        Staff lastStaff = staffService.getLastUsername(corpUid);
        if (null != lastStaff) {
            String lastUsername = lastStaff.getUsername();
            // 在最后一次出现"yyMm"后获取其之后的字符串
            String lastString = StringUtils.substringAfterLast(lastUsername, corpUid);
            if (StringUtils.isBlank(lastString))
                return 0;
            int num = Integer.parseInt(lastString);
            usernameNum = num + 1;
            // 数字最后一位是4则跳过
            usernameNum = TextUtils.dealLastFour(usernameNum);
        }
        return usernameNum;
    }

    /**
     * 创建员工
     */
    private Staff newStaff(
            String corpUid, String storeUid, long storeId, String staffUid, String username, String fullName, String phone,
            String auth, String encodedPwd, String pwdSalt
    ) {
        Staff staff = new Staff();
        staff.setCorpUid(corpUid);
        staff.setStoreId(storeId);
        staff.setCurrentStoreUid(storeUid);
        staff.setUid(staffUid);
        staff.setUsername(username);
        staff.setFullName(fullName);
        staff.setNickName(fullName);
        staff.setPriPhone(phone);
        staff.setPubPhone(phone);
        staff.setAuthApps(auth);
        staff.setEncodedPwd(encodedPwd);
        staff.setPwdSalt(pwdSalt);
        staff.setEnabled(1);
        return staff;
    }

    /**
     * 创建员工角色
     */
    private StaffRoles newStaffRole(String staffUid, String storeUid, String roleName, String roles, String headStaffUid, String headStaffName) {
        StaffRoles staffRoles = new StaffRoles();
        staffRoles.setStoreUid(storeUid);
        staffRoles.setStaffUid(staffUid);
        staffRoles.setIsHeadquarterStaff(0);
        staffRoles.setIsStoreStaff(1);
        staffRoles.setInStoreService(1);
        staffRoles.setRoleName(roleName);
        staffRoles.setRoles(roles);
        staffRoles.setEnabled(1);
        staffRoles.setAddStaffUid(headStaffUid);
        staffRoles.setAddStaffName(headStaffName);
        staffRoles.setLastStaffUid(headStaffUid);
        staffRoles.setLastStaffName(headStaffName);
        return staffRoles;
    }

    private void dealInClass(
            List<SsMstXcxAnswersPandect> ssXcxAnswersPandectList, List<MstXcxAnswersPandect> mstXcxAnswersPandectList,
            List<SsMstXcxStudentQuizAnswers> ssXcxStudentQuizAnswersList, List<MstXcxStudentQuizAnswers> mstXcxStudentQuizAnswersList,
            Map<String, Members> membersMap
    ) {
        for (SsMstXcxAnswersPandect xcxAnswersPandect : ssXcxAnswersPandectList) {
            Members members = membersMap.get(xcxAnswersPandect.getStudentUid());
            MstXcxAnswersPandect mstXcxAnswersPandect = new MstXcxAnswersPandect();
            mstXcxAnswersPandect.setUid(xcxAnswersPandect.getUid());
            mstXcxAnswersPandect.setSubjectLessonUid(xcxAnswersPandect.getSubjectLessonUid());
            mstXcxAnswersPandect.setStudentUid(members.getUid());
            mstXcxAnswersPandect.setVersionUid(xcxAnswersPandect.getVersionUid());
            mstXcxAnswersPandect.setIsAllCorrect(xcxAnswersPandect.getIsAllCorrect());
            mstXcxAnswersPandect.setEnabled(xcxAnswersPandect.getEnabled());
            mstXcxAnswersPandect.setAddStaffUid(staffUid);
            mstXcxAnswersPandect.setAddStaffName(staffName);
            mstXcxAnswersPandect.setLastStaffUid(staffUid);
            mstXcxAnswersPandect.setLastStaffName(staffName);
            mstXcxAnswersPandectList.add(mstXcxAnswersPandect);
        }
        for (SsMstXcxStudentQuizAnswers xcxStudentQuizAnswers : ssXcxStudentQuizAnswersList) {
            Members members = membersMap.get(xcxStudentQuizAnswers.getStudentUid());
            MstXcxStudentQuizAnswers mstXcxStudentQuizAnswers = new MstXcxStudentQuizAnswers();
            mstXcxStudentQuizAnswers.setUid(xcxStudentQuizAnswers.getUid());
            mstXcxStudentQuizAnswers.setAgencyUid(corpUid);
            mstXcxStudentQuizAnswers.setStoreUid(storeUid);
            mstXcxStudentQuizAnswers.setSubjectLessonUid(xcxStudentQuizAnswers.getSubjectLessonUid());
            mstXcxStudentQuizAnswers.setStudentUid(members.getUid());
            mstXcxStudentQuizAnswers.setStudentOptionUid(xcxStudentQuizAnswers.getStudentOptionUid());
            mstXcxStudentQuizAnswers.setQuestionUid(xcxStudentQuizAnswers.getQuestionUid());
            mstXcxStudentQuizAnswers.setIsCorrect(xcxStudentQuizAnswers.getIsCorrect());
            mstXcxStudentQuizAnswers.setVersionUid(xcxStudentQuizAnswers.getVersionUid());
            mstXcxStudentQuizAnswers.setEnabled(xcxStudentQuizAnswers.getEnabled());
            mstXcxStudentQuizAnswers.setProjectUid(xcxStudentQuizAnswers.getProjectUid());
            mstXcxStudentQuizAnswers.setStageUid(xcxStudentQuizAnswers.getStageUid());
            mstXcxStudentQuizAnswers.setStudentName(members.getMemberName());
            mstXcxStudentQuizAnswers.setPhone(members.getMob());
            mstXcxStudentQuizAnswers.setAddStaffUid(staffUid);
            mstXcxStudentQuizAnswers.setAddStaffName(staffName);
            mstXcxStudentQuizAnswers.setLastStaffUid(staffUid);
            mstXcxStudentQuizAnswers.setLastStaffName(staffName);
            mstXcxStudentQuizAnswersList.add(mstXcxStudentQuizAnswers);
        }
    }

    private void dealAfterClass(
            List<SsMstXcxStudentHomework> ssMstXcxStudentHomeworkList, List<MstXcxStudentHomework> mstXcxStudentHomeworkList,
            List<SsMstXcxHomeworkFiles> ssMstXcxHomeworkFilesList, List<MstXcxHomeworkFiles> mstXcxHomeworkFilesList,
            List<SsMstXcxHomeworkFilesTrack> ssMstXcxHomeworkFilesTrackList, List<MstXcxHomeworkFilesTrack> mstXcxHomeworkFilesTrackList,
            Map<String, Members> membersMap
    ) {
        for (SsMstXcxStudentHomework ssMstXcxStudentHomework : ssMstXcxStudentHomeworkList) {
            Members members = membersMap.get(ssMstXcxStudentHomework.getStudentUid());
            MstXcxStudentHomework mstXcxStudentHomework = new MstXcxStudentHomework();
            mstXcxStudentHomework.setUid(ssMstXcxStudentHomework.getUid());
            mstXcxStudentHomework.setAgencyUid(corpUid);
            mstXcxStudentHomework.setStoreUid(storeUid);
            mstXcxStudentHomework.setSubjectLessonUid(ssMstXcxStudentHomework.getSubjectLessonUid());
            mstXcxStudentHomework.setQuestionUid(ssMstXcxStudentHomework.getQuestionUid());
            mstXcxStudentHomework.setStudentUid(members.getUid());
            mstXcxStudentHomework.setStatus(ssMstXcxStudentHomework.getStatus());
            mstXcxStudentHomework.setSubmissionTime(ssMstXcxStudentHomework.getSubmissionTime());
            mstXcxStudentHomework.setTeacherScore(ssMstXcxStudentHomework.getTeacherScore());
            mstXcxStudentHomework.setEvaluationText(ssMstXcxStudentHomework.getEvaluationText());
            //todo
            mstXcxStudentHomework.setEvaluationStaffUid(ssMstXcxStudentHomework.getEvaluationStaffUid());
            mstXcxStudentHomework.setEvaluationTime(ssMstXcxStudentHomework.getEvaluationTime());
            mstXcxStudentHomework.setVersionUid(ssMstXcxStudentHomework.getVersionUid());
            mstXcxStudentHomework.setEnabled(ssMstXcxStudentHomework.getEnabled());
            mstXcxStudentHomework.setProjectUid(ssMstXcxStudentHomework.getProjectUid());
            mstXcxStudentHomework.setStageUid(ssMstXcxStudentHomework.getStageUid());
            mstXcxStudentHomework.setStudentName(members.getMemberName());
            mstXcxStudentHomework.setPhone(members.getMob());
            mstXcxStudentHomework.setAddStaffUid(staffUid);
            mstXcxStudentHomework.setAddStaffName(staffName);
            mstXcxStudentHomework.setLastStaffUid(staffUid);
            mstXcxStudentHomework.setLastStaffName(staffName);
            mstXcxStudentHomeworkList.add(mstXcxStudentHomework);
        }
        for (SsMstXcxHomeworkFiles ssMstXcxHomeworkFiles : ssMstXcxHomeworkFilesList) {
            MstXcxHomeworkFiles mstXcxHomeworkFiles = new MstXcxHomeworkFiles();
            mstXcxHomeworkFiles.setUid(ssMstXcxHomeworkFiles.getUid());
            mstXcxHomeworkFiles.setHomeworkUid(ssMstXcxHomeworkFiles.getHomeworkUid());
            mstXcxHomeworkFiles.setUrl(ssMstXcxHomeworkFiles.getUrl());
            mstXcxHomeworkFiles.setVersionUid(ssMstXcxHomeworkFiles.getVersionUid());
            mstXcxHomeworkFiles.setEnabled(ssMstXcxHomeworkFiles.getEnabled());
            mstXcxHomeworkFiles.setAddStaffUid(staffUid);
            mstXcxHomeworkFiles.setAddStaffName(staffName);
            mstXcxHomeworkFiles.setLastStaffUid(staffUid);
            mstXcxHomeworkFiles.setLastStaffName(staffName);
            mstXcxHomeworkFilesList.add(mstXcxHomeworkFiles);
        }
        for (SsMstXcxHomeworkFilesTrack ssMstXcxHomeworkFilesTrack : ssMstXcxHomeworkFilesTrackList) {
            Members members = membersMap.get(ssMstXcxHomeworkFilesTrack.getStudentUid());
            MstXcxHomeworkFilesTrack mstXcxHomeworkFilesTrack = new MstXcxHomeworkFilesTrack();
            mstXcxHomeworkFilesTrack.setUid(ssMstXcxHomeworkFilesTrack.getUid());
            mstXcxHomeworkFilesTrack.setUrl(ssMstXcxHomeworkFilesTrack.getUrl());
            mstXcxHomeworkFilesTrack.setStudentUid(members.getUid());
            mstXcxHomeworkFilesTrack.setSubjectLessonUid(ssMstXcxHomeworkFilesTrack.getSubjectLessonUid());
            mstXcxHomeworkFilesTrack.setHomeworkUid(ssMstXcxHomeworkFilesTrack.getHomeworkUid());
            mstXcxHomeworkFilesTrack.setStatus(ssMstXcxHomeworkFilesTrack.getStatus());
            mstXcxHomeworkFilesTrack.setCleanupTime(ssMstXcxHomeworkFilesTrack.getCleanupTime());
            mstXcxHomeworkFilesTrack.setAddStaffUid(staffUid);
            mstXcxHomeworkFilesTrack.setAddStaffName(staffName);
            mstXcxHomeworkFilesTrack.setLastStaffUid(staffUid);
            mstXcxHomeworkFilesTrack.setLastStaffName(staffName);
            mstXcxHomeworkFilesTrackList.add(mstXcxHomeworkFilesTrack);
        }
    }

    private void dealLearningProgress(
            List<SsMstXcxStudentLearningProgress> ssMstXcxStudentLearningProgressList, List<MstXcxStudentLearningProgress> mstXcxStudentLearningProgressList,
            Map<String, Members> membersMap
    ) {
        for (SsMstXcxStudentLearningProgress ssMstXcxStudentLearningProgress : ssMstXcxStudentLearningProgressList) {
            Members members = membersMap.get(ssMstXcxStudentLearningProgress.getStudentUid());
            MstXcxStudentLearningProgress mstXcxStudentLearningProgress = new MstXcxStudentLearningProgress();
            mstXcxStudentLearningProgress.setUid(ssMstXcxStudentLearningProgress.getUid());
            mstXcxStudentLearningProgress.setAgencyUid(corpUid);
            mstXcxStudentLearningProgress.setStoreUid(storeUid);
            mstXcxStudentLearningProgress.setStudentUid(members.getUid());
            mstXcxStudentLearningProgress.setSubjectLessonUid(ssMstXcxStudentLearningProgress.getSubjectLessonUid());
            mstXcxStudentLearningProgress.setQuestionUid(ssMstXcxStudentLearningProgress.getQuestionUid());
            mstXcxStudentLearningProgress.setHomeworkUid(ssMstXcxStudentLearningProgress.getHomeworkUid());
            mstXcxStudentLearningProgress.setMaterialStage(ssMstXcxStudentLearningProgress.getMaterialStage());
            mstXcxStudentLearningProgress.setStudentOptionUid(ssMstXcxStudentLearningProgress.getStudentOptionUid());
            mstXcxStudentLearningProgress.setMaterialFileUid(ssMstXcxStudentLearningProgress.getMaterialFileUid());
            mstXcxStudentLearningProgress.setDurationSeconds(ssMstXcxStudentLearningProgress.getDurationSeconds());
            mstXcxStudentLearningProgress.setVersionUid(ssMstXcxStudentLearningProgress.getVersionUid());
            mstXcxStudentLearningProgress.setEnabled(ssMstXcxStudentLearningProgress.getEnabled());
            mstXcxStudentLearningProgress.setProjectUid(ssMstXcxStudentLearningProgress.getProjectUid());
            mstXcxStudentLearningProgress.setStageUid(ssMstXcxStudentLearningProgress.getStageUid());
            mstXcxStudentLearningProgress.setStudentName(members.getMemberName());
            mstXcxStudentLearningProgress.setPhone(members.getMob());
            mstXcxStudentLearningProgress.setAddStaffUid(staffUid);
            mstXcxStudentLearningProgress.setAddStaffName(staffName);
            mstXcxStudentLearningProgress.setLastStaffUid(staffUid);
            mstXcxStudentLearningProgress.setLastStaffName(staffName);
            mstXcxStudentLearningProgressList.add(mstXcxStudentLearningProgress);
        }
    }


}
